package com.tank;

import java.awt.*;

public class Bullet {
    private  static  final  int SPEED = 10;
    public   static  int WIDTH=ResourceMgr.bulletD.getWidth();
    public  static  int HEIGHT = ResourceMgr.bulletD.getHeight();

    private int x, y;
    private TankFrame tf;
    private Dir dir;
    private Tank bullettank;
    private Rectangle rectbullet = new Rectangle(0, 0, WIDTH, HEIGHT);

    private  boolean living = true;

    public Bullet(int x, int y, Dir dir, Tank bullettank, TankFrame tf) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.tf = tf;
        this.bullettank = bullettank;

        this.rectbullet.x = x;
        this.rectbullet.y = y;

    }

    public void Paint(Graphics g) {
        if (!living){
            tf.bullets.remove(this);
        }
        switch (dir){
            case LEFT:
                g.drawImage(ResourceMgr.bulletL, x, y, null);
                break;
            case UP:
                g.drawImage(ResourceMgr.bulletU, x, y, null);
                break;
            case RIGHT:
                g.drawImage(ResourceMgr.bulletR, x, y, null);
                break;
            case DOWN:
                g.drawImage(ResourceMgr.bulletD, x, y, null);;
                break;
        }
        move();
    }

    private void move() {
        switch (dir){
            case LEFT:
                x-=SPEED;
                break;
            case UP:
                y-=SPEED;
                break;
            case RIGHT:
                x+=SPEED;
                break;
            case DOWN:
                y+=SPEED;
                break;
        }

        this.rectbullet.x = x;
        this.rectbullet.y = y;

        if (x< 0 || y < 0 || x > tf.GAME_WIDTH || y > tf.GAME_HEIGHT)
            living = false;
    }

    public void collidwith(Tank tank) {

        if (bullettank.getGroup() == tank.getGroup()) return;;

        //todo:用一个rect来记录位置
        //Rectangle rectbullet = new Rectangle(this.x, this.y, WIDTH, HEIGHT);
        //Rectangle recttank = new Rectangle(tank.getX(), tank.getY(), tank.getWIDTH(), tank.getHEIGHT());

        if (rectbullet.intersects(tank.getRecttank())){
            tank.die();
            this.living = false;
        }
    }
}
